package com.ecs.server.importdata;

import java.text.SimpleDateFormat;

import org.hibernate.classic.Session;

import com.ecs.message.Acceleration;
import com.ecs.message.BatteryVoltage;
import com.ecs.message.CarbonDioxide;
import com.ecs.message.Humidity;
import com.ecs.message.Illuminance;
import com.ecs.message.Motion;
import com.ecs.message.Temperature;

public class InjectData {
	
	public void inject(XMLMessage xmlMessage, Session session) throws Exception
	{
		if(xmlMessage.getMeasurementType() == null)
		{
			return;
		}
		else if(xmlMessage.getNodeId().equals("100"))
		{
			return;
		}
		else if(xmlMessage.getMeasurementType().equalsIgnoreCase("Temperature"))
		{
			Temperature temp = new Temperature();
			temp.setNetworkId(xmlMessage.getNetworkId());
			temp.setNodeId(xmlMessage.getNodeId());
			temp.setSensorId(xmlMessage.getSensorId());
			temp.setValue(xmlMessage.getNormalValue());
			temp.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(xmlMessage.getTime()));
			org.hibernate.Transaction tx = session.beginTransaction();
			session.save(temp);
			tx.commit();
			session.close();
//			System.out.println("Succcefully inject data!");
		}
		else if(xmlMessage.getMeasurementType().equalsIgnoreCase("Acceleration"))
		{
			Acceleration temp = new Acceleration();
			temp.setNetworkId(xmlMessage.getNetworkId());
			temp.setNodeId(xmlMessage.getNodeId());
			temp.setSensorId(xmlMessage.getSensorId());
			temp.setTotal(xmlMessage.getNormalValue());
			temp.setPitch(xmlMessage.getPitch());
			temp.setZ(xmlMessage.getZ());
			temp.setY(xmlMessage.getY());
			temp.setX(xmlMessage.getX());
			temp.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(xmlMessage.getTime()));
			org.hibernate.Transaction tx = session.beginTransaction();
			session.save(temp);
			tx.commit();
			session.close();
//			System.out.println("Succcefully inject data!");
		}
		else if(xmlMessage.getMeasurementType().equalsIgnoreCase("Battery voltage"))
		{
			BatteryVoltage temp = new BatteryVoltage();
			temp.setNetworkId(xmlMessage.getNetworkId());
			temp.setNodeId(xmlMessage.getNodeId());
			temp.setSensorId(xmlMessage.getSensorId());
			temp.setValue(xmlMessage.getNormalValue());
			temp.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(xmlMessage.getTime()));
			org.hibernate.Transaction tx = session.beginTransaction();
			session.save(temp);
			tx.commit();
			session.close();
//			System.out.println("Succcefully inject data!");
		}
		else if(xmlMessage.getMeasurementType().equalsIgnoreCase("Carbon dioxide"))
		{
			CarbonDioxide temp = new CarbonDioxide();
			temp.setNetworkId(xmlMessage.getNetworkId());
			temp.setNodeId(xmlMessage.getNodeId());
			temp.setSensorId(xmlMessage.getSensorId());
			temp.setValue(xmlMessage.getNormalValue());
			temp.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(xmlMessage.getTime()));
			org.hibernate.Transaction tx = session.beginTransaction();
			session.save(temp);
			tx.commit();
			session.close();
//			System.out.println("Succcefully inject data!");
		}
		else if(xmlMessage.getMeasurementType().equalsIgnoreCase("Illuminance"))
		{
			Illuminance temp = new Illuminance();
			temp.setNetworkId(xmlMessage.getNetworkId());
			temp.setNodeId(xmlMessage.getNodeId());
			temp.setSensorId(xmlMessage.getSensorId());
			temp.setValue(xmlMessage.getNormalValue());
			temp.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(xmlMessage.getTime()));
			org.hibernate.Transaction tx = session.beginTransaction();
			session.save(temp);
			tx.commit();
			session.close();
//			System.out.println("Succcefully inject data!");
		}
		else if(xmlMessage.getMeasurementType().equalsIgnoreCase("Humidity"))
		{
			Humidity temp = new Humidity();
			temp.setNetworkId(xmlMessage.getNetworkId());
			temp.setNodeId(xmlMessage.getNodeId());
			temp.setSensorId(xmlMessage.getSensorId());
			temp.setValue(xmlMessage.getNormalValue());
			temp.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(xmlMessage.getTime()));
			org.hibernate.Transaction tx = session.beginTransaction();
			session.save(temp);
			tx.commit();
			session.close();
//			System.out.println("Succcefully inject data!");
		}
		else if(xmlMessage.getMeasurementType().equalsIgnoreCase("Motion"))
		{
			Motion temp = new Motion();
			temp.setNetworkId(xmlMessage.getNetworkId());
			temp.setNodeId(xmlMessage.getNodeId());
			temp.setSensorId(xmlMessage.getSensorId());
			temp.setValue(xmlMessage.getNormalValue());
			temp.setDate(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(xmlMessage.getTime()));
			org.hibernate.Transaction tx = session.beginTransaction();
			session.save(temp);
			tx.commit();
			session.close();
//			System.out.println("Succcefully inject data!");
		}
		
	}
}
